/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Galaktika.BI.Runtime
{
    public class SafeDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    {
        private Dictionary<TKey, TValue> m_InnerData;
        public SafeDictionary()
        {
            m_InnerData = new Dictionary<TKey, TValue>();
        }

        public SafeDictionary(KeyValuePair<TKey, TValue>[] values)
            : this()
        {
            foreach (KeyValuePair<TKey, TValue> kv in values)
            {
                m_InnerData.Add(kv.Key, kv.Value);
            }
        }

        public SafeDictionary(IDictionary<TKey, TValue> values)
            : this()
        {
            foreach (KeyValuePair<TKey, TValue> kv in values)
            {
                m_InnerData.Add(kv.Key, kv.Value);
            }
        }

        #region IDictionary<TKey,TValue> Members

        public void Add(TKey key, TValue value)
        {
            m_InnerData.Add(key, value);
        }

        public bool ContainsKey(TKey key)
        {
            return m_InnerData.ContainsKey(key);
        }

        public ICollection<TKey> Keys
        {
            get { return m_InnerData.Keys; }
        }

        public bool Remove(TKey key)
        {
            return m_InnerData.Remove(key);
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            return m_InnerData.TryGetValue(key, out value);
        }

        public ICollection<TValue> Values
        {
            get { return m_InnerData.Values; }
        }

        public TValue this[TKey key]
        {
            get
            {
                TValue res;
                m_InnerData.TryGetValue(key, out res);
                return res;
            }
            set
            {
                TValue res;
                m_InnerData.TryGetValue(key, out res);
                if (res == null)
                {
                    m_InnerData.Add(key, value);
                }
                else
                {
                    m_InnerData[key] = value;
                }
            }
        }

        #endregion

        #region ICollection<KeyValuePair<TKey,TValue>> Members

        public void Add(KeyValuePair<TKey, TValue> item)
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).Add(item);
        }

        public void Clear()
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).Clear();
        }

        public bool Contains(KeyValuePair<TKey, TValue> item)
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).Contains(item);
        }

        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).Count; }
        }

        public bool IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).IsReadOnly; }
        }

        public bool Remove(KeyValuePair<TKey, TValue> item)
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)m_InnerData).Remove(item);
        }

        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return ((IEnumerable<KeyValuePair<TKey, TValue>>)m_InnerData).GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)m_InnerData).GetEnumerator();
        }

        #endregion
    }
}
